<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="收货姓名" prop="receiverName">
      <el-input v-model="dataForm.receiverName" placeholder="收货姓名"></el-input>
    </el-form-item>
    <el-form-item label="收货固定电话" prop="receiverPhone">
      <el-input v-model="dataForm.receiverPhone" placeholder="收货固定电话"></el-input>
    </el-form-item>
    <el-form-item label="收货移动电话" prop="receiverMobile">
      <el-input v-model="dataForm.receiverMobile" placeholder="收货移动电话"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="receiverProvince">
      <el-input v-model="dataForm.receiverProvince" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="receiverCity">
      <el-input v-model="dataForm.receiverCity" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="区/县" prop="receiverDistrict">
      <el-input v-model="dataForm.receiverDistrict" placeholder="区/县"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="receiverAddress">
      <el-input v-model="dataForm.receiverAddress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="receiverZip">
      <el-input v-model="dataForm.receiverZip" placeholder="邮编"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          receiverName: '',
          receiverPhone: '',
          receiverMobile: '',
          receiverProvince: '',
          receiverCity: '',
          receiverDistrict: '',
          receiverAddress: '',
          receiverZip: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          receiverName: [
            { required: true, message: '收货姓名不能为空', trigger: 'blur' }
          ],
          receiverPhone: [
            { required: true, message: '收货固定电话不能为空', trigger: 'blur' }
          ],
          receiverMobile: [
            { required: true, message: '收货移动电话不能为空', trigger: 'blur' }
          ],
          receiverProvince: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          receiverCity: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          receiverDistrict: [
            { required: true, message: '区/县不能为空', trigger: 'blur' }
          ],
          receiverAddress: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          receiverZip: [
            { required: true, message: '邮编不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shop/goodsshipping/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.goodsshipping.userId
                this.dataForm.receiverName = data.goodsshipping.receiverName
                this.dataForm.receiverPhone = data.goodsshipping.receiverPhone
                this.dataForm.receiverMobile = data.goodsshipping.receiverMobile
                this.dataForm.receiverProvince = data.goodsshipping.receiverProvince
                this.dataForm.receiverCity = data.goodsshipping.receiverCity
                this.dataForm.receiverDistrict = data.goodsshipping.receiverDistrict
                this.dataForm.receiverAddress = data.goodsshipping.receiverAddress
                this.dataForm.receiverZip = data.goodsshipping.receiverZip
                this.dataForm.createTime = data.goodsshipping.createTime
                this.dataForm.updateTime = data.goodsshipping.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/goodsshipping/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'receiverName': this.dataForm.receiverName,
                'receiverPhone': this.dataForm.receiverPhone,
                'receiverMobile': this.dataForm.receiverMobile,
                'receiverProvince': this.dataForm.receiverProvince,
                'receiverCity': this.dataForm.receiverCity,
                'receiverDistrict': this.dataForm.receiverDistrict,
                'receiverAddress': this.dataForm.receiverAddress,
                'receiverZip': this.dataForm.receiverZip,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
